﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLZJFurnitureSCM.Model;
using System.Data.Linq;
using System.Data.SqlClient;
using System.Data;

namespace QLZJFurnitureSCM.DAL
{
    public class ContactDAO:IBaseDAO<Contact>
    {
        private string connectionString = DALFactory.CONNECTIONSTRING;

        private SqlConnection sqlConn = null;

        public ContactDAO()
        {
            sqlConn = new SqlConnection(connectionString);
        }

        public Contact Create(Contact t)
        {
            try
            {
                string query = string.Format("INSERT INTO [Contacts]([contactName],[contactPhone],[contactAddress],[contactDiscount])VALUES ('{0}','{1}','{2}',{3}); select @@identity;",
                                                        t.ContactName, t.ContactPhone, t.ContactAddress, t.ContactDiscount);
                if (sqlConn.State != ConnectionState.Open)
                    sqlConn.Open();
                SqlCommand sqlCmd = new SqlCommand(query, sqlConn);
                Console.WriteLine(query);
                object o = sqlCmd.ExecuteScalar();
                sqlConn.Close();
                if (o != null)
                    t.ContactId = int.Parse(o.ToString());
            }
            catch (System.Exception ex)
            {
                
                Console.WriteLine(ex);
                return Create(t);
            }
            return t;
        }

        public Contact Read(Contact t)
        {
            throw new NotImplementedException();
        }

        public bool Update(Contact t)
        {
            try
            {
                string query = string.Format("update Contacts set contactPhone = '{0}', contactAddress = '{1}', contactDiscount = {2} where contactId = {3};", t.ContactPhone, t.ContactAddress, t.ContactDiscount, t.ContactId);
                Console.WriteLine("Update Query: " + query);
                DataContext dataContext = new DataContext(connectionString);
                dataContext.ExecuteQuery<Contact>(query);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }

        public bool Delete(Contact t)
        {
            try
            {
                string query = string.Format("delete Contacts where contactId = {0};", t.ContactId);
                Console.WriteLine("Update Query: " + query);
                DataContext dataContext = new DataContext(connectionString);
                dataContext.ExecuteQuery<Contact>(query);
            }
            catch (System.Exception)
            {
                return false;
            }
            return true;
        }

        public List<Contact> ReadAll()
        {
            DataContext dataContext = new DataContext(connectionString);
            IEnumerable<Contact> enumContact = dataContext.ExecuteQuery<Contact>("select * from Contacts;");
            return enumContact.ToList();
        }
    }
}
